package com.egg.basic.config.sms;

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.teaopenapi.models.Config;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

/**
 * 阿里短信相关配置
 *
 * @author east
 */
@Profile({"prod", "test", "dev"})
@Slf4j
@Data
@Configuration
@ConfigurationProperties(prefix = "sms.ali")
public class SmsAliConfig {


    @ApiModelProperty(value = "AccessKeyID")
    private String accessKeyId;

    @ApiModelProperty(value = "AccessKeySecret")
    private String accessKeySecret;

    @ApiModelProperty(value = "域名")
    private String endpoint;


    @Bean
    public Client initClient() {
        Config config = new Config();
        config.setAccessKeyId(getAccessKeyId());
        config.setAccessKeySecret(getAccessKeySecret());
        config.setEndpoint(getEndpoint());
        Client client;
        try {
            client = new Client(config);
            log.info("初始化成功 {}", Client.class.getName());
            return client;
        } catch (Exception e) {
            log.error("初始化失败 {} 原因 {}", Client.class.getName(), e.getMessage());
        }
        return null;
    }


}
